<?php

namespace app\admin\controller;

use app\common\controller\BaseAdmin;
use app\traits\Result;
use think\facade\Filesystem;
use think\exception\ValidateException;

class Upload extends BaseAdmin
{

    /**
     * 单张图片上传
     * @return string
     * @throws \Exception
     */
    public function image(){
        $file = request()->file('file');
        // 上传到本地服务器
        $type = request()->get('type');
        try {
            //验证只能传图片防止上传可执行PHP代码
            validate(['file'=>'fileSize:8102000|fileExt:jpg,png,jpeg,gif'])->check(request()->file());
            $savename = Filesystem::disk('public')->putFile($type, $file);
            $path = '/uploads/'.$savename;

            //如果是编辑器上传
            if ($type == 'layedit'){
                return Result::error('上传成功',null,['src'=>$path]);
            }
            return Result::success('上传成功',null,$path);
        } catch (ValidateException $e) {
            return Result::error($e->getMessage());
        }
    }

    /**
     * 单张视频上传
     * @return string
     * @throws \Exception
     */
    public function excel(){
        $file = request()->file('file');

        // 上传到本地服务器
        $type = request()->get('type');
        try {
            //验证只能传图片防止上传可执行PHP代码
            validate(['file'=>'fileSize:8102000|fileExt:xlsx,xls'])->check(request()->file());
            $savename = Filesystem::disk('public')->putFile($type, $file);
            $path = '/uploads/'.$savename;

            //如果是编辑器上传
            if ($type == 'layedit'){
                return Result::error('上传成功',null,['src'=>$path]);
            }
            return Result::success('上传成功',null,$path);
        } catch (ValidateException $e) {
            return Result::error($e->getMessage());
        }
    }


}